package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.studentManager.model.WritePaper;
import com.hdoit.kylin.logical.studentManager.service.IWritePaperService;
import com.hdoit.kylin.logical.viewmodel.postgraduate.WritePaperSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      anshengqiang
 * @email       
 * @createtime  2017年08月01日 19:45:34
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/postgraduate/writePaper/")
public class WritePaperController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/postgraduate/writePaper/";
    }

    private IWritePaperService writePaperService;
    public IWritePaperService getWritePaperService() {
        return writePaperService;
    }
    @Resource
    public void setWritePaperService(IWritePaperService writePaperService) {
        this.writePaperService = writePaperService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="writePaperDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,WritePaperSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(writePaperService.queryCount(search));
        List<WritePaper> writePapers= writePaperService.queryList(search,pagerModel);
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("writePapers", writePapers);
        model.addAttribute("writePaperSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String writePaperDetailed(Model model,String key){
        WritePaper writePaper=new WritePaper();
        if(!StringUtils.isEmpty(key)){
            writePaper=writePaperService.queryModel(key);
        }
        model.addAttribute("writePaper", writePaper);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,WritePaper writePaper){
        if(StringUtils.isEmpty(writePaper.getId())){
            writePaper.setId(CommonUtils.getKeyId());
            writePaper.setModelStatus(ModelStatus.add);
        }else{
            writePaper.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(writePaperService.applyChange(writePaper)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("writePaper", writePaper);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(writePaperService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}